#include "widget.h"
#include "ui_widget.h"

#include <QPushButton>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

//    // test1：设置带图标的按钮
//    QPushButton* btn = new QPushButton("带图标的按钮",this);
//    btn->move(200,200);
//    btn->setFixedSize(200,100);
//    // 0.创建图标对象
//    QIcon icon(":/people.jpg");
//    // 1.先设置图标
//    btn->setIcon(icon);
//    // 2.在设置图标的大小
//    btn->setIconSize(QSize(100,100));

    // test2：设置带快捷键的按钮
    // 1.创建按钮
    QPushButton* target = new QPushButton(this);
    QPushButton* up     = new QPushButton(this);
    QPushButton* down   = new QPushButton(this);
    QPushButton* left   = new QPushButton(this);
    QPushButton* right  = new QPushButton(this);

    // 设置target的唯一对象名
    target->setObjectName("PushButton_Target");

    // 2.设置按钮位置和尺寸
    target->move(350,50);
    target->setFixedSize(100,100);

    up->move(350,250);
    up->setFixedSize(100,100);

    down->move(350,450);
    down->setFixedSize(100,100);

    left->move(250,350);
    left->setFixedSize(100,100);

    right->move(450,350);
    right->setFixedSize(100,100);

    // 3.为按钮添加图标，并设置图标的大小
    target->setIcon(QIcon(":/dog.png"));
    target->setIconSize(QSize(100,100));
    up->setIcon(QIcon(":/up.png"));
    up->setIconSize(QSize(100,100));
    down->setIcon(QIcon(":/down.png"));
    down->setIconSize(QSize(100,100));
    left->setIcon(QIcon(":/left.png"));
    left->setIconSize(QSize(100,100));
    right->setIcon(QIcon(":/right.png"));
    right->setIconSize(QSize(100,100));

    // 4.为按钮设置快捷键
    up->setShortcut(QKeySequence("w"));
    down->setShortcut(QKeySequence("s"));
    left->setShortcut(QKeySequence("a"));
    right->setShortcut(QKeySequence("d"));

    // 5.为操作按钮添加槽函数，并关联信号与槽
    connect(up, &QPushButton::clicked, this, &Widget::upSlot);
    connect(down, &QPushButton::clicked, this, &Widget::downSlot);
    connect(left, &QPushButton::clicked, this, &Widget::leftSlot);
    connect(right, &QPushButton::clicked, this, &Widget::rightSlot);

    // 6.设置重复触发
    up->setAutoRepeat(true);
    down->setAutoRepeat(true);
    left->setAutoRepeat(true);
    right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::upSlot()
{
    QPushButton* findButton = this->findChild<QPushButton*>("PushButton_Target");
    if(findButton != nullptr)
    {
        QRect rect = findButton->geometry();
        findButton->setGeometry(rect.x(), rect.y()-5, rect.width(), rect.height());
        qDebug() << "up";
    }
}
void Widget::downSlot()
{
    QPushButton* findButton = this->findChild<QPushButton*>("PushButton_Target");
    if(findButton != nullptr)
    {
        QRect rect = findButton->geometry();
        findButton->setGeometry(rect.x(), rect.y()+5, rect.width(), rect.height());
        qDebug() << "down";
    }
}
void Widget::leftSlot()
{
    QPushButton* findButton = findChild<QPushButton*>("PushButton_Target");
    if(findButton != nullptr)
    {
        QRect rect = findButton->geometry();
        findButton->setGeometry(rect.x()-5, rect.y(), rect.width(), rect.height());
        qDebug() << "left";
    }
}
void Widget::rightSlot()
{
    QPushButton* findButton = findChild<QPushButton*>("PushButton_Target");
    if(findButton != nullptr)
    {
        QRect rect = findButton->geometry();
        findButton->setGeometry(rect.x()+5, rect.y(), rect.width(), rect.height());
        qDebug() << "right";
    }
}

